<?php
namespace App\Models;

class NotifyLog extends BaseModel
{

    protected $table = 'notify_log';

    protected $casts = [
        'notify' => 'array'
    ];

    public function shop()
    {
        return $this->belongsTo('App\Models\Shop', 'shop_id', 'id');
    }

    public function getLogList()
    {
        $where = [];
        $keyword = request('keyword');
        if ($keyword) {
            if (is_numeric($keyword) && strlen($keyword) < 10) {
                $where['mid'] = $keyword;
            } else {
                $where['order_sn'] = ['like', '%' . $keyword . '%'];
            }
        }
        $rows = $this->_getJoinPageList('shop', $where, '*', ['id' => 'desc'], 15,10, 'id,shop_name');
        if ($rows['data']) {
            $txtStr['type'] = [1 => '零售', 2 => '水票', 3 => '续费'];
            $rows['data'] = int_to_string($rows['data'], $txtStr);
        }
        return $rows;
    }

    public function add($type = 1, $notify, $errmsg)
    {
        $data['type'] = $type;
        $data['order_sn'] = isset($notify['terminal_trace']) ? $notify['terminal_trace'] :$notify['out_trade_no'];
        $data['notify'] = $notify;
        $data['errmsg'] = $errmsg;

        $model = new Order();
        if ($type == 2) {
            $model = new waterBillOrder();
        }
        if ($type == 3) {
            $model = new serviceCharge();
        }
        $res = $model->_getOne(['order_sn' => $data['order_sn']]);
        if (!$res) {
            $this->_save($data);
            return false;
        }
        if ($type == 1) {
            $data['mid'] = $res['buyer_id'];
            $data['admin_id'] = $res['admin_id'];
            $data['shop_id'] = $res['shop_id'];
        }
        if ($type == 2) {
            $data['mid'] = $res['mid'];
            $data['admin_id'] = $res['admin_id'];
            $data['shop_id'] = $res['shop_id'];
        }
        if ($type == 3) {
            $data['admin_id'] = $res['admin_id'];
            $data['shop_id'] = $res['shop_id'];
        }
        return $this->_save($data);
    }

    public function clear()
    {
        $this->_del();
    }

}
